package cn.raven.pet.service.impl;

import cn.raven.basic.constant.BaseConstant;
import cn.raven.basic.utils.BaiduAuditUtils;
import cn.raven.pet.domain.Pet;
import cn.raven.pet.domain.PetDetail;
import cn.raven.pet.domain.PetOnlineAuditLog;
import cn.raven.pet.mapper.PetDetailMapper;
import cn.raven.pet.mapper.PetMapper;
import cn.raven.pet.mapper.PetOnlineAuditLogMapper;
import cn.raven.pet.mapper.PetTypeMapper;
import cn.raven.pet.service.IPetService;
import cn.raven.basic.service.impl.BaseServiceImpl;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Raven
 * @since 2022-07-16
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    /**
     * @Title: add
     * @Description: 级联保存详情表
     * @Author: king
     * @Version: 1.0
     * @Date: 2022/7/16 20:35
     * @Parameters: [pet]
     * @Return void
     */
    @Override
    public void add(Pet pet) {
        //新增pet基本信息，返回自增id
        petMapper.add(pet);
        PetDetail detail = pet.getDetail();
        //给petId赋值
        detail.setPetId(pet.getId());
        //保存
        petDetailMapper.add(detail);
    }

    /**
     * @Title: batchDel
     * @Description: 批量级联删除详情表
     * @Author: king
     * @Version: 1.0
     * @Date: 2022/7/16 21:19
     * @Parameters: [ids]
     * @Return void
     */
    @Override
    public void batchDel(Long[] ids) {
        petMapper.batchDel(ids);
        petDetailMapper.batchDel(ids);
    }

    /**
     * @Title: update
     * @Description: 级联修改详情表
     * @Author: king
     * @Version: 1.0
     * @Date: 2022/7/16 20:36
     * @Parameters: [pet]
     * @Return void
     */
    @Override
    public void update(Pet pet) {
        petMapper.update(pet);
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        detail.setAdoptNotice(detail.getAdoptNotice());
        detail.setIntro(detail.getIntro());
        petDetailMapper.updateByPetId(detail);
    }

    /**
     * @Title: del
     * @Description: 级联删除详情表
     * @Author: king
     * @Version: 1.0
     * @Date: 2022/7/16 20:36
     * @Parameters: [id]
     * @Return void
     */
    @Override
    public void del(Long id) {
        petMapper.del(id);
        petDetailMapper.delByPetId(id);
    }

    /**
     * @Title: onsale
     * @Description: 批量上架
     * @Author: king
     * @Version: 1.0
     * @Date: 2022/7/16 17:35
     * @Parameters: [ids]
     * @Return void
     */
    @Override
    public void onsale(List<Long> ids) {
        //遍历ids
        List<Long> pIds = new ArrayList<>();
        for (Long id : ids) {
            Pet pet = petMapper.findOne(id);
            if (pet.getState() == 1) {
                continue;
            }
            StringBuffer stringBuffer = new StringBuffer();
            if (!BaiduAuditUtils.TextCensor(pet.getName())) {
                stringBuffer.append("宠物名称不合规，宠物名：" + pet.getName());
            }
            String[] split = pet.getResources().split(",");
            for (String picture : split) {
                String url = BaseConstant.FastDfsConstant.FASTDFS_PREFIX + picture;
                if (!BaiduAuditUtils.ImgCensor(url)) {
                    stringBuffer.append("宠物图片不合规，图片路径：" + url);
                }
            }
            if (stringBuffer.length() > 0) {
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(id);
                petOnlineAuditLog.setNote(stringBuffer.toString());
                petOnlineAuditLogMapper.add(petOnlineAuditLog);
                continue;
            }
            pIds.add(id);
        }
        if (pIds.size() > 0) {
            petMapper.onsale(pIds);
        }
    }

    /**
     * @Title: offsale
     * @Description: 批量下架
     * @Author: king
     * @Version: 1.0
     * @Date: 2022/7/16 17:35
     * @Parameters: [ids]
     * @Return void
     */
    @Override
    public void offsale(List<Long> ids) {
        petMapper.offsale(ids);
    }

    @Override
    public PetDetail findByPetId(Long petId) {
        return petDetailMapper.findByPetId(petId);
    }
}
